﻿@namespace Masa.Blazor
@using Masa.Blazor.Components.Pagination
@using StyleBuilder = Masa.Blazor.Core.StyleBuilder
@inherits ThemeComponentBase

<CascadingValue Value="ComputedTheme" Name="MasaBlazorCascadingTheme">
    <nav id="@Id" @ref="Ref" class="@Class" style="@Style"
         role="navigation"
         @attributes="@Attributes">
        <ul class="@_modifierBuilder.Add(Circle).Add(Disabled).AddTheme(ComputedTheme)">
            @GenPaginationIcon(PaginationIconType.First)

            @if (_internalMiniVariant)
            {
                <div class="m-pagination__mini">
                    <span class="m-pagination__current">@Value</span>
                    <span class="m-pagination__slash">/</span>
                    <span class="m-pagination__total">@Length</span>
                </div>
            }
            else
            {
                @foreach (var item in GetItems())
                {
                    <li>
                        @if (item.IsT0)
                        {
                            <span class="@_block.Element("more")">@item</span>
                        }
                        else
                        {
                            @GenPaginationItem(item)
                        }
                    </li>
                }
            }

            @GenPaginationIcon(PaginationIconType.Second)
        </ul>
    </nav>
</CascadingValue>

@code {

    private RenderFragment GenPaginationIcon(PaginationIconType iconType) => __builder =>
    {
        var tag = "a";
        var disabled = iconType == PaginationIconType.First ? PrevDisabled : NextDisabled;
        var icon = GetIcon(iconType);
        var value = iconType == PaginationIconType.First ? Value - 1 : Value + 1;
        Func<MouseEventArgs, Task> click = icon == PrevIcon ? HandlePreviousAsync : HandleNextAsync;

        Dictionary<string, object> attrs = new()
        {
            ["__internal_preventDefault_onclick"] = true
        };

        if (HrefFormat is null)
        {
            tag = "button";
            attrs["type"] = "button";
        }
        else if (!disabled)
        {
            attrs["href"] = string.Format(HrefFormat, value);
        }

        <MElement Tag="@tag"
                  Class="@_navigationModifierBuilder.Add(disabled).AddElevation(Elevation).Build()"
                  @onclick="@click"
                  @attributes="@attrs">
            <MIcon Icon="@icon"/>
        </MElement>
    };

    private RenderFragment GenPaginationItem(StringNumber item) => __builder =>
    {
        var tag = "a";
        Dictionary<string, object> attrs = new()
        {
            ["__internal_preventDefault_onclick"] = true
        };

        if (HrefFormat is null)
        {
            tag = "button";
            attrs["type"] = "button";
        }
        else
        {
            attrs["href"] = string.Format(HrefFormat, item);
        }

        var isActive = item.AsT1 == Value;
        var elevation = isActive ? Elevation + 2 : Elevation;
        
        <MElement Tag="@tag"
                  Class="@_itemModifierBuilder.Add("active", isActive).AddElevation(elevation).AddBackgroundColor(Color, isActive).Build()"
                  Style="@(StyleBuilder.Create().AddBackgroundColor(Color, isActive).ToString())"
                  @onclick="@(() => HandleItemClickAsync(item))"
                  @attributes="@attrs">
            @item
        </MElement>
    };

}